import os
import time
import json
import pandas as pd
from tqdm import tqdm
from pycocotools.coco import COCO

def trans_id(category_id):
    names = []
    namesid = []
    for i in range(0, len(cats)):
        names.append(cats[i]['name'])
        namesid.append(cats[i]['id'])
        #print('id:{1}\t {0}'.format(names[i], namesid[i]))
    index = namesid.index(category_id)
    return index

anno  = 'slot_ocr_rotate_6187.json'

coco = COCO(anno)  # 读文件
cats = coco.loadCats(coco.getCatIds())  # 这里loadCats就是coco提供的接口，获取类别


with open(anno, 'r') as load_f:
    f = json.load(load_f)

imgs = f['images']

txtpath = 'slot_ocr_filename.txt'
f1 = open(txtpath, 'w')

for i in tqdm(range(len(imgs))):
    xml_content = []
    file_name = imgs[i]['file_name']
    f1.write(file_name)
    f1.write('\n')
f1.close()

import os
import shutil

source_txt = "slot_ocr_filename.txt"
dest_folder = ""

with open(source_txt, "r") as f:
    lines = f.readlines()
    for line in lines:
        abs_path = line.strip()
        if os.path.exists(abs_path):
            filename = os.path.basename(abs_path)
            dest_path = os.path.join(dest_folder, filename)
            shutil.copy(abs_path, dest_path)
            print(f"Copy file {abs_path} to {dest_path} successfully!")
        else:
            print(f"File {abs_path} does not exist!")